﻿using System.Runtime.Serialization;

namespace RockBus.DataContracts
{
    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public class ReceivePortInfo : EvaluationElementInfoBase<ReceivePortDetail>
    {
        public ReceivePortInfo(
            PublisherInfo publisherInfo,
            ReceivePortDetail receivePortDetail,
            string responseId = null,
            bool generateDefaultResponse = false)
            : base(receivePortDetail)
        {
            this.PublisherInfo = publisherInfo;
            this.Detail = receivePortDetail;
            this.ResponseId = responseId;
            this.GenerateDefaultResponse = generateDefaultResponse;
        }

        [IgnoreDataMember]
        public PublisherInfo PublisherInfo { get; set; }

        [DataMember]
        public string ResponseId { get; set; }

        [DataMember]
        public bool GenerateDefaultResponse { get; set; }

        [IgnoreDataMember]
        public EndpointInfo ReceiveEndpointInfo
        {
            get
            {
                if ((this.Detail == null) || (string.IsNullOrEmpty(this.Detail.EndpointId)))
                {
                    return null;
                }

                if (!this.PublisherInfo.EndpointInfoDictionary.ContainsKey(this.Detail.EndpointId))
                {
                    return null;
                }

                return this.PublisherInfo.EndpointInfoDictionary[this.Detail.EndpointId];
            }
        }

        [IgnoreDataMember]
        public ResponseInfo ResponseInfo
        {
            get
            {
                if (string.IsNullOrEmpty(this.ResponseId))
                {
                    return null;
                }

                if (!this.PublisherInfo.ResponseInfoDictionary.ContainsKey(this.ResponseId))
                {
                    return null;
                }

                return this.PublisherInfo.ResponseInfoDictionary[this.ResponseId];
            }
        }

        public PublisherReceivePortInfo GetPublisherReceivePortInfo()
        {
            return new PublisherReceivePortInfo(this.PublisherInfo.Id, this.Id);
        }

        public override string ToString()
        {
            return string.Format("ReceivePortInfo: PublisherId={0}; {1}",
                this.PublisherInfo.Id,
                this.ReceiveEndpointInfo.ToString());
        }
    }
}